//
//  CJAssetsController.m
//  Boss
//
//  Created by admin on 2018/9/18.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJAssetsController.h"
#import "CJAllAssetsCell.h"
#import "CJAssetDistributionCell.h"
#import "CJAssetTrendCell.h"
#import "CJAssetsModel.h"
#import "CJForwardRevenusController.h"
#import "CJAllForwardController.h"
#import "WYPurseDetailController.h"
#import "WYPutForwardController.h"
@interface CJAssetsController ()<UITableViewDelegate,UITableViewDataSource,CJAllAssetsCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)CJAssetsModel *assetsModel;
@end

@implementation CJAssetsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}

-(void)loadData
{
    WEAKSELF
    [WYHttpServer getEarningsSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            weakSelf.assetsModel = [CJAssetsModel getAssetsModelFromJson:response[@"result"]];
            [weakSelf.tableView reloadData];
        }
        
        
        
    }];
}
-(void)setupUI
{
    self.navigationItem.title = @"总资产";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"资产明细" style:(UIBarButtonItemStyleDone) target:self action:@selector(assetDetail)];
    
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 300;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CJAllAssetsCell" bundle:nil] forCellReuseIdentifier:@"CJAllAssetsCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CJAssetDistributionCell" bundle:nil] forCellReuseIdentifier:@"CJAssetDistributionCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CJAssetTrendCell" bundle:nil] forCellReuseIdentifier:@"CJAssetTrendCell"];
    
    self.tableView.backgroundColor = kColorMainColor;
    
    
    UIView *footerView = [[UIView alloc]init];
    footerView.frame = CGRectMake(10, 0, kScreenWidth-20, 100);
    footerView.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *shareBtn = [[UIButton alloc]init];
    [footerView addSubview:shareBtn];
    
    shareBtn.frame  = CGRectMake(30, 25, kScreenWidth-60, 50);
    [shareBtn setBackgroundColor:kColorMainColor];
    [shareBtn setTitle:@"晒晒我的资产" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
   // self.tableView.tableFooterView  = footerView;
    
    
    
    
    
}
-(void)assetDetail
{
    WYPurseDetailController *pdc = [[WYPurseDetailController alloc]init];
    [self.navigationController pushViewController:pdc animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        CJAllAssetsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJAllAssetsCell" forIndexPath:indexPath];
        
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJAllAssetsCell" owner:nil options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        if (self.assetsModel) {
            [cell setTotalAmount:self.assetsModel.totalAmount];
        }
        cell.delegate = self;
        
        return cell;
    }
    
    if (indexPath.section ==1) {
        CJAssetDistributionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJAssetDistributionCell" forIndexPath:indexPath];
        
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJAssetDistributionCell" owner:nil options:nil].firstObject;
        }
        if (self.assetsModel) {
            cell.allPrice = self.assetsModel.totalAmount.totalAmount;
            cell.property = self.assetsModel.property;
        }
        
        
        
        cell.backgroundColor = [UIColor clearColor];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.section==2) {
        CJAssetTrendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJAssetTrendCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJAssetTrendCell" owner:nil options:nil].firstObject;
        }
        cell.backgroundColor = [UIColor clearColor];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.assetsModel) {
            [cell setThirty:self.assetsModel.thirty];
            [cell setSeven:self.assetsModel.seven];
        }
        
        
        
        return cell;
    }
    
    
    
    
    return nil;
}

-(void)didPutForwardAction
{
    WYPutForwardController *pfc =[[WYPutForwardController alloc]init];
    pfc.blance = self.assetsModel.totalAmount.balance;
    [self.navigationController pushViewController:pfc animated:YES];
}



#pragma  mark - CJAllAssetsCellDelegate
-(void)didAllForward
{
    CJAllForwardController *allVc =[[CJAllForwardController alloc]init];
    [self.navigationController pushViewController:allVc animated:YES];
}
-(void)didForwardRevenue
{
    CJForwardRevenusController *frc = [[CJForwardRevenusController alloc]init];
    [self.navigationController pushViewController:frc animated:YES];
}




- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame;
        if (_isme) {
              frame= CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        }else{
              frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight-self.tabHeight);
        }
       
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}



@end
